package com.efrei.schoolmanager.views;

import java.awt.Dimension;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.efrei.schoolmanager.views.components.CustomLabel;

public class VueAccueil extends JPanel {
	private static final long serialVersionUID = 5609533397615811010L;

	private final static String TITLE = "School Manager : bienvenue";

	private JPanel presentationPanel, contenuPanel, titlePanel, planPanel,
			gestionAdminPanel, gestionAcadPanel, documentsPanel, logoPanel;
	private static final Dimension DIM = new Dimension(300, 200);
	// Labels
	JLabel titleLabel, planLabel, logoLabel;
	CustomLabel ajoutEleveLabel, eleveLabel, listeElevesLabel,
			listeClassesLabel, gestionNotesLabel, convocationAbsLabel,
			convocationResponsablesLabel, bulletinAbsLabel,
			certificatScolariteLabel, releveNotesLabel, bulletinNotesLabel,
			demandeBourseLabel;

	public VueAccueil() {
		super();
		initLabels();
		remplirTitlePanel();
		remplirPlanPanel();
		remplirDocumentsPanel();
		remplirGestionAcadPanel();
		remplirGestionAdminPanel();
		remplirPresentationPanel();
		remplirLogoPanel();
		remplirContenuPanel();
		remplirMainPanel();
		configurerVue();
		setVisible(true);

	}

	private void initLabels() {
		// Permet d'indenter le texte
		final String indent = "    ";
		final int reductionPolice = 2;
		titleLabel = new JLabel(TITLE);
		titleLabel.setFont((new Font("Arial", Font.BOLD, 30)));
		planLabel = new JLabel("Plan de l'application: ");
		planLabel.setFont((new Font("Arial", Font.BOLD, 22)));
		ajoutEleveLabel = new CustomLabel("- Ajouter un élève");
		eleveLabel = new CustomLabel("- Eleve");
		listeElevesLabel = new CustomLabel(indent + "> Lister les élèves");
		listeElevesLabel.reduirePolice(reductionPolice);
		listeClassesLabel = new CustomLabel(indent + "> Lister les classes");
		listeClassesLabel.reduirePolice(reductionPolice);
		gestionNotesLabel = new CustomLabel("- Gerer les notes");
		convocationAbsLabel = new CustomLabel("- Convocation pour absences");
		convocationResponsablesLabel = new CustomLabel(
				"- Convocation de responsables");
		bulletinAbsLabel = new CustomLabel("- Bulletin d'absences");
		certificatScolariteLabel = new CustomLabel("- Certificat de scolarité");
		releveNotesLabel = new CustomLabel("- Relevé de notes périodique");
		bulletinNotesLabel = new CustomLabel("- Bulletin de notes");
		demandeBourseLabel = new CustomLabel("- Demande de bourse");
		ImageIcon icon = new ImageIcon("images/logo.jpg");
		int imgHeight = icon.getIconHeight();
		int imgWidth = icon.getIconWidth();
		// Image de l'application
		logoLabel = new JLabel(icon);
		logoLabel.setPreferredSize(new Dimension(imgWidth, imgHeight));
		Dimension dimLogo = new Dimension(imgWidth / 7, imgHeight / 7);
		logoLabel.setSize(dimLogo);
		logoLabel.setMinimumSize(dimLogo);
		logoLabel.setMaximumSize(dimLogo);
	}

	private void remplirTitlePanel() {
		titlePanel = new JPanel();
		titlePanel.add(titleLabel);

	}

	private void remplirPlanPanel() {
		planPanel = new JPanel();
		planPanel.add(planLabel);

	}

	private void remplirDocumentsPanel() {
		documentsPanel = new JPanel();
		BoxLayout layout = new BoxLayout(documentsPanel, BoxLayout.PAGE_AXIS);
		documentsPanel.setLayout(layout);
		Box box = Box.createVerticalBox();
		box.add(convocationAbsLabel);
		box.add(convocationResponsablesLabel);
		box.add(bulletinAbsLabel);
		box.add(certificatScolariteLabel);
		box.add(releveNotesLabel);
		box.add(bulletinNotesLabel);
		box.add(demandeBourseLabel);
		box.add(Box.createGlue());
		box.setBorder(BorderFactory.createTitledBorder("Documents imprimables"));
		box.setPreferredSize(DIM);
		box.setMinimumSize(DIM);
		documentsPanel.add(box);

	}

	private void remplirGestionAcadPanel() {
		gestionAcadPanel = new JPanel();
		// Gestionnaire de positionnement
		BoxLayout layout = new BoxLayout(gestionAcadPanel, BoxLayout.PAGE_AXIS);
		gestionAcadPanel.setLayout(layout);
		gestionAcadPanel.add(gestionNotesLabel);
		Box box = Box.createVerticalBox();
		box.add(gestionNotesLabel);
		box.add(Box.createGlue());
		box.setBorder(BorderFactory.createTitledBorder("Gestion academique"));
		box.setPreferredSize(DIM);
		box.setMinimumSize(DIM);
		gestionAcadPanel.add(box);
	}

	private void remplirGestionAdminPanel() {
		gestionAdminPanel = new JPanel();
		// Gestionnaire de positionnement
		BoxLayout layout = new BoxLayout(gestionAdminPanel, BoxLayout.PAGE_AXIS);
		gestionAdminPanel.setLayout(layout);
		Box box = Box.createVerticalBox();
		box.add(ajoutEleveLabel);
		box.add(eleveLabel);
		box.add(listeElevesLabel);
		box.add(listeClassesLabel);
		box.add(Box.createGlue());
		box.setBorder(BorderFactory
				.createTitledBorder("Gestion administrative"));
		box.setPreferredSize(DIM);
		box.setMinimumSize(DIM);
		gestionAdminPanel.add(box);

	}

	private void remplirPresentationPanel() {
		presentationPanel = new JPanel();
		BoxLayout layout = new BoxLayout(presentationPanel, BoxLayout.PAGE_AXIS);
		presentationPanel.setLayout(layout);
		Box box = Box.createVerticalBox();
		box.add(titlePanel);
		box.add(Box.createRigidArea(new Dimension(0, 50)));
		box.add(planPanel);
		presentationPanel.add(box);
	}

	private void remplirContenuPanel() {
		contenuPanel = new JPanel();

		// Gestionnaire de positionnement
		GroupLayout gl = new GroupLayout(contenuPanel);
		contenuPanel.setLayout(gl);
		// Espacement des composants
		gl.setAutoCreateGaps(true);
		// Espacement entre composants et conteneur
		gl.setAutoCreateContainerGaps(true);

		// Groupe sequentiel sur l'axe horizontal
		GroupLayout.SequentialGroup hGroup = gl.createSequentialGroup();

		// Constitution des groupes paralleles horizontalement
		/* 1e colonne */
		hGroup.addGroup(gl.createParallelGroup()
				.addComponent(gestionAdminPanel));
		/* 2e colonne */
		hGroup.addGroup(gl.createParallelGroup().addComponent(gestionAcadPanel));
		/* 3e colonne */
		hGroup.addGroup(gl.createParallelGroup().addComponent(documentsPanel));
		gl.setHorizontalGroup(hGroup);

		// Groupe sequentiel sur l'axe vertical
		GroupLayout.SequentialGroup vGroup = gl.createSequentialGroup();

		// Constitution des groupes paralleles verticalement
		/* 1e ligne */
		vGroup.addGroup(gl.createParallelGroup(Alignment.BASELINE)
				.addComponent(gestionAdminPanel).addComponent(gestionAcadPanel)
				.addComponent(documentsPanel));
		gl.setVerticalGroup(vGroup);
	}

	private void remplirLogoPanel() {
		logoPanel = new JPanel();
		logoPanel.add(logoLabel);

	}

	private void remplirMainPanel() {
		BoxLayout layout = new BoxLayout(this, BoxLayout.PAGE_AXIS);
		setLayout(layout);
		Box box = Box.createVerticalBox();
		box.add(presentationPanel);
		box.add(Box.createRigidArea(new Dimension(0, 50)));
		box.add(contenuPanel);
		box.add(logoPanel);
		this.add(box);

	}

	/**
	 * Configurer les options de la vue
	 * 
	 */
	private void configurerVue() {
		// Permet redimensionnement de l'écran
		Dimension preferedSize = getPreferredSize();
		Dimension dimensionIdeale = new Dimension(preferedSize.width,
				preferedSize.height + 28);
		setMinimumSize(dimensionIdeale);
		setSize(dimensionIdeale);
	}

	public String getTitle() {
		return TITLE;
	}

}
